package com.geekaca.web.servlet;

import com.geekaca.web.pojo.Brand;
import com.geekaca.web.service.BrandService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.List;

@WebServlet(urlPatterns = "/brands")
public class BrandServlet extends HttpServlet {
    private BrandService brandService = new BrandService();

    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println("Brand servlet...");
        // 登录确认
        HttpSession session = req.getSession();
//        Object loggedObj = session.getAttribute("logged");
//
//        System.out.println("loggedobj: " + loggedObj);
//        String logged = loggedObj.toString();
//        System.out.println("logged: " + logged);
//
//        if (!logged.equals("1")) {
//            resp.setHeader("Content-type", "text/html; charset=utf-8");
//            PrintWriter writer = resp.getWriter();
//            writer.write("<h1>非法请求，请先登录</h1>");
//            return;
//        }



        // 确定页码
        String pageStr = req.getParameter("page");
        System.out.println("page: " + pageStr);
        Integer page;
        if (pageStr == null) {
            page = 1;
        } else {
            page = Integer.parseInt(pageStr);
            System.out.println("curPage: " + page);
        }


        List<Brand> brands = brandService.getBrandsByPage(page);

        req.setAttribute("page", page);
        System.out.println("page:" + page);


        req.setAttribute("brands", brands);
        req.getRequestDispatcher("/Brand.jsp").forward(req, resp);
//        }


    }
}
